//
//  TeacherInfoViewController.m
//  ZPIOSEducation
//
//  Created by Encoder on 17/1/19.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "TeacherInfoViewController.h"
#import "TeacherInfoTableViewCell.h"
#import "ZPInfoIndexMapText.h"

@interface TeacherInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *mItems;
}
@property (weak, nonatomic) IBOutlet UITableView *tableview;

@end

@implementation TeacherInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"基本信息";
    [self handleData];
    [_tableview registerNib:[UINib nibWithNibName:@"TeacherInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"TeacherInfoTableViewCell"];
    _tableview.rowHeight=UITableViewAutomaticDimension;
    _tableview.estimatedRowHeight=70;
    [_tableview setTableFooterView:[UIView new]];
}

- (void) handleData
{
    NSMutableDictionary *dicContainer = [NSMutableDictionary changeType:_dic];
    NSArray *allKey = [dicContainer allKeys];
    mItems = [[NSMutableArray alloc]init];
    BOOL isShow = [[dicContainer objectForKey:@"showPhone"]boolValue];
    for (int i = 0; i<allKey.count; i++) {
        NSString *key = [allKey objectAtIndex:i];
        if(![key isEqualToString:@"showPhone"] && ![key isEqualToString:@"studyRecord"] ){
            NSString *value = [_dic objectForKey:key];
            if([value length] != 0){
                if([key isEqualToString:@"graduation"]){
                    ZPInfoIndexMapText *mapText =  [[ZPInfoIndexMapText alloc]initWithLeftText:@"大学" rightText:value index:7];
                    [mItems addObject:mapText];
                }else if ([key isEqualToString:@"introduce"]){
                    ZPInfoIndexMapText *mapText =  [[ZPInfoIndexMapText alloc]initWithLeftText:@"介绍" rightText:value index:8];
                    [mItems addObject:mapText];
                }else if ([key isEqualToString:@"schoolName"]){
                    ZPInfoIndexMapText *mapText =  [[ZPInfoIndexMapText alloc]initWithLeftText:@"教学点" rightText:value index:1];
                    [mItems addObject:mapText];
                }else if ([key isEqualToString:@"studyRecordString"]){
                    ZPInfoIndexMapText *mapText =  [[ZPInfoIndexMapText alloc]initWithLeftText:@"学历" rightText:value index:6];
                    [mItems addObject:mapText];
                }else if ([key isEqualToString:@"subject"]){
                    ZPInfoIndexMapText *mapText =  [[ZPInfoIndexMapText alloc]initWithLeftText:@"科目" rightText:value index:2];
                    [mItems addObject:mapText];
                }else if ([key isEqualToString:@"teacherName"]){
                    ZPInfoIndexMapText *mapText =  [[ZPInfoIndexMapText alloc]initWithLeftText:@"姓名" rightText:value index:0];
                    [mItems addObject:mapText];
                }else if ([key isEqualToString:@"teacherPhone"]){
                    if(isShow){
                        ZPInfoIndexMapText *mapText =  [[ZPInfoIndexMapText alloc]initWithLeftText:@"手机" rightText:value index:4];
                        [mItems addObject:mapText];
                    }
                }else if ([key isEqualToString:@"wechat"]){
                    ZPInfoIndexMapText *mapText =  [[ZPInfoIndexMapText alloc]initWithLeftText:@"微信" rightText:value index:5];
                    [mItems addObject:mapText];
                }else if ([key isEqualToString:@"grade"]){
                    ZPInfoIndexMapText *mapText =  [[ZPInfoIndexMapText alloc]initWithLeftText:@"年级" rightText:value index:3];
                    [mItems addObject:mapText];
                }
            }
        }
    }
    mItems = [mItems sortedArrayUsingComparator:^NSComparisonResult(ZPInfoIndexMapText *obj1,ZPInfoIndexMapText *obj2) {
        NSComparisonResult result =   [[NSString stringWithFormat:@"%ld",(long)obj1.itemIndex] compare:[NSString stringWithFormat:@"%ld",(long)obj2.itemIndex]];
        return result;
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return mItems.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TeacherInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TeacherInfoTableViewCell"];
    ZPInfoIndexMapText *mapText = [mItems objectAtIndex:indexPath.row];
    cell.item.text = mapText.leftText;
    cell.content.text = mapText.rightText;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==4) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",toStr([_dic objectForKey:@"teacherPhone"])]]];
    }
}

@end
